package com.pzb.web.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.core.page.TableDataInfo;
import com.pzb.order.domain.CheckOutModel;
import com.pzb.order.domain.OdOrder;
import com.pzb.order.domain.OdOrderDetail;
import com.pzb.order.service.IOdOrderService;
import com.pzb.wxpay.util.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单管理Controller
 * 
 * @author Mr_Wu
 * @date 2021-12-15
 */
@RestController
@RequestMapping("/order")
public class OdOrderController extends BaseController
{
    @Autowired
    private IOdOrderService odOrderService;


    /**
     * 结算台订单信息
     */
    @GetMapping("/order")
    public AjaxResult createOrder(CheckOutModel model)
    {
        model.setOpenid(getOpenid());
        AjaxResult result = odOrderService.createOrder(model);
        return result;
    }

    /**
     * 结算台提交订单/
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody CheckOutModel model, HttpServletRequest request){

        model.setOpenid(getOpenid());
        model.setIp(PayUtil.getIpAddr(request));
        AjaxResult result = odOrderService.submitOrder(model);
        return result;
    }

    /**
     * 获取订单数量/
     */
    @GetMapping("/todoCounts")
    public AjaxResult todoCounts(@RequestParam String extAppid)
    {
        JSONObject object = new JSONObject();
        //待支付
        object.put("payment",odOrderService.todoOrderCount(getOpenid(),extAppid,"0","0"));
        //待发货
        object.put("delivery",odOrderService.todoOrderCount(getOpenid(),extAppid,"0","1"));
        //待收货
        object.put("received",odOrderService.todoOrderCount(getOpenid(),extAppid,"1",null));
        return AjaxResult.success(object);
    }

    /**
     * 结算台提交订单/
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("dataType") String dataType, @RequestParam("extAppid") String extAppid)
    {
        OdOrder q=new OdOrder();
        q.setExtAppid(extAppid);
        q.setOpenid(getOpenid());
        if("payment".equals(dataType)){//待支付
            q.setOrderStatus("0");
            q.setPayStatus("0");
        }else if("delivery".equals(dataType)){//待发货
            q.setOrderStatus("0");
            q.setPayStatus("1");
        }else if("received".equals(dataType)){//待收货
            q.setOrderStatus("1");
        }else if("comment".equals(dataType)){
            q.setOrderStatus("2");
        }
        startPage();
        List<OdOrder> orderList = odOrderService.selectOdOrderList(q);
        return getDataTable(orderList);
    }

    /**
     * 订单支付
     */
    @GetMapping("/pay")
    public AjaxResult pay(@RequestParam Long orderId,@RequestParam String payType, @RequestParam String extAppid,HttpServletRequest request)
    {
        AjaxResult result = odOrderService.pay(orderId,payType,extAppid,getOpenid(),PayUtil.getIpAddr(request));
        return result;
    }

    /**
     * 订单详情
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam Long orderId)
    {
        return AjaxResult.success(odOrderService.selectOdOrderById(orderId));
    }

    /**
     * 确认收货
     */
    @PostMapping("/receipt")
    public AjaxResult receipt(@RequestBody Map param)
    {
        Long orderId = Long.parseLong(param.get("orderId").toString());
        OdOrder order = odOrderService.selectOdOrderById(orderId);
        order.setOrderStatus("2");
        order.setFinishTime(new Date());
        return toAjax(odOrderService.updateOdOrder(order));
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody Map param)
    {
        Long orderId = Long.parseLong(param.get("orderId").toString());
        return toAjax(odOrderService.cancelOrder(orderId));
    }

    /**
     * 获取订单待评价商品列表
     */
    @GetMapping("/comment/list")
    public AjaxResult commentList(@RequestParam Long orderId)
    {
        List<OdOrderDetail> orderDetailList = odOrderService.commentList(orderId);
        return AjaxResult.success(orderDetailList);
    }

    /**
     * 获取订单待评价商品列表
     */
    @PostMapping("/comment/submit")
    public AjaxResult commentSubmit(@RequestBody OdOrder order)
    {
        int count = odOrderService.commentSubmit(order);
        return toAjax(count);
    }


    /**
     * 获取订单待评价商品列表
     */
    @GetMapping("/express")
    public AjaxResult express(@RequestParam Long orderId)
    {
        return AjaxResult.success("该功能暂未开放,敬请期待！");
    }

}
